//
//  UUMyInfoVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 14/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUMyInfoVC.h"
#import "UUBaseTableViewCell.h"
#import <UUFoundation/UIImage+Extensions.h>
#import "UUResourceManager.h"
#import "UUUserManager.h"
#import "UUPermissionManager.h"
#import <UUFoundation/DKTitleView.h>
#import "UUSettingsVC.h"
#import "UUPermissionUtility.h"
#import "UUProductVC.h"
#import "UUProductPackageVC.h"

@interface UUMyInfoProductCell : UUBaseTableViewCell

@property (nonatomic, weak) IBOutlet UIImageView *charterProductImageView;
@property (nonatomic, weak) IBOutlet UIImageView *lineProductImageView;

@end

@implementation UUMyInfoProductCell

@end

////////////////////////////////////////////////////////////////////////

@interface UUMyInfoVC () <UUUserManagerObserver>

@property (nonatomic, strong) DKOffsetTitleView *titleView;

@property (nonatomic, weak) IBOutlet UIView *headerView;
@property (nonatomic, weak) IBOutlet UIImageView *iconImageView;
@property (nonatomic, weak) IBOutlet UILabel *nicknameLabel;
@property (nonatomic, weak) IBOutlet UILabel *permissionLabel;
@property (nonatomic, weak) IBOutlet UILabel *balanceLabel;
@property (nonatomic, weak) IBOutlet UILabel *freezeLabel;
@property (nonatomic, weak) IBOutlet UILabel *reviewStatusLabel;

@end

@implementation UUMyInfoVC

+ (BOOL)enableTranslucentNavigationBar {
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.headerView.backgroundColor = COMMON_TRANSLUCENT_NAVIGATIONBAR_COLOR;

    [self.translucentNavigationBar setBackgroundImage:[UIImage imageFromColor:COMMON_TRANSLUCENT_NAVIGATIONBAR_COLOR]
                                       forBarPosition:UIBarPositionTopAttached
                                           barMetrics:UIBarMetricsDefault];
    self.translucentNavigationBar.translucent = NO;
    
    DKStickyHeaderView *stickyHeaderView = [[DKStickyHeaderView alloc] initWithMinHeight:0];
    stickyHeaderView.backgroundColor = COMMON_TRANSLUCENT_NAVIGATIONBAR_COLOR;
    [self.tableView addSubview:stickyHeaderView];
    
    [self setRightBarButtonItem:[UIBarButtonItem transparentBarButtonItemWithTitle:@"设置"
                                                                            target:self
                                                                          selector:@selector(settings:)]];
    
    self.titleView = [[DKOffsetTitleView alloc] initWithTitle:@"我的"];
    [self setupBarTitleView:self.titleView];

    self.nicknameLabel.textColor = RGB_COLOR(154, 167, 230);
    self.permissionLabel.textColor = RGB_COLOR(161, 157, 175);
    self.balanceLabel.textColor = COMMON_TEXT_RED_COLOR;
    self.freezeLabel.textColor = COMMON_TEXT_LIGHT_COLOR;
    self.reviewStatusLabel.textColor = COMMON_TEXT_RED_COLOR;
    self.iconImageView.layer.cornerRadius = self.iconImageView.width / 2;
    self.iconImageView.layer.masksToBounds = YES;
    
    [GetUserManager() addObserver:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    WEAK_SELF();
    [GetUserManager() asyncFetchUserInfoWithCompleteBlock:^(UUUserInfo *userInfo, NSError *error) {
        if (error == nil) {
            [weakSelf updateUIWithUserInfo:userInfo];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)settings:(id)sender {
    [self.navigationController pushViewController:[UUSettingsVC instanceOfAppropriate] animated:YES];
}

- (IBAction)charterProduct:(id)sender {
    [self.navigationController pushViewController:[UUProductVC instanceOfAppropriate] animated:YES];
}

- (IBAction)lineProduct:(id)sender {
    [self.navigationController pushViewController:[UUProductPackageVC instanceOfAppropriate] animated:YES];
}

- (void)updateUIWithUserInfo:(UUUserInfo *)userInfo {
    [self.iconImageView uu_setImageWithURLString:userInfo.avatar];
    
    self.nicknameLabel.text = userInfo.company;
    self.permissionLabel.text = [NSString stringWithFormat:@"%@ %@",
                                 userInfo.name,
                                 [UUPermissionUtility roleNameForRoleId:userInfo.roleId]];
    
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]
                                                 initWithString:[userInfo.withdrawMoney toCurrency] attributes:@{
                                                                                                                     NSFontAttributeName : [UIFont systemFontOfSize:19]
                                                                                                                     }];
    [attributedText setAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} range:NSMakeRange(0, 1)];
    self.balanceLabel.attributedText = attributedText;
    
    self.freezeLabel.text = [NSString stringWithFormat:@"冻结金额 %@", [userInfo.freezeMoney toCurrency]];
    
    switch (userInfo.status) {
        case UUSupplierReviewStatusRejected:
            self.reviewStatusLabel.text = @"审核不通过";
            break;
        case UUSupplierReviewStatusWaitingForReview:
            self.reviewStatusLabel.text = @"待审核";
            break;
        default:
            self.reviewStatusLabel.text = @"";
            break;
    }
}

#pragma mark - UITableViewDelegate & UITableViewDataSource methods

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && ![GetPermissionManager() checkPermission:UUPermissionFlagWallet]) {
        return 0;
    } else {
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    
    if ([cell isKindOfClass:[UUMyInfoProductCell class]]) {
        UUMyInfoProductCell *productCell = (UUMyInfoProductCell *)cell;
        productCell.charterProductImageView.image = GetResourceManager().charterProductImage;
        productCell.lineProductImageView.image = GetResourceManager().lineProductImage;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == [self numberOfSectionsInTableView:tableView] - 1 &&
        indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1) {
        [UIAlertController presentAlertWithPhone:@"15759239680"];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.titleView setTitleOffsetWithScrollView:scrollView];
}

#pragma mark - UUUserManagerObserver methods

- (void)userDidUpdate {
    [self.iconImageView uu_setImageWithURLString:GetUserManager().fetchUserInfo.avatar];
}

@end
